#include "stdafx.h"
#include "ReactorPool.h"
#include "Reactor.h"


Reactor * ReactorPool::Create( const ReactorDef* pDef )
{
	m_iCounter++;
	if (m_qFreeList.empty())
	{ 
		AllocateChunk(); 
	}

	Reactor* temp = m_qFreeList.front();
	m_qFreeList.pop();

	temp = new(temp) Reactor(pDef);
	temp->m_iID=m_iCounter;
	return temp;
}
ReactorPool::ReactorPool()
{
	AllocateChunk();
	m_iCounter=0;
}

void ReactorPool::AllocateChunk()
{
	char * pChunk=new char[sizeof(Reactor)*cuChunkSize];
	m_vChunks.push_back(pChunk);
	for (int i = 0 ; i < cuChunkSize ; i++)
	{
		Reactor* pTemp = (Reactor*)(pChunk+i*sizeof(Reactor));
		m_qFreeList.push(pTemp);
	}
}

void ReactorPool::Release( Reactor * pReactor )
{
	pReactor->~Reactor();
	m_qFreeList.push(pReactor);
}

ReactorPool::~ReactorPool()
{
	for(uint32 ii=0;ii<m_vChunks.size();ii++)
	{
		delete [] m_vChunks[ii];
	}
}